﻿using System;
using ApprovalTests;
using ApprovalTests.Reporters;
using ApprovalUtilities.Utilities;
using Microsoft.VisualStudio.TestTools.UnitTesting;

namespace SmallBasicUtils.Tests
{
	[TestClass]
	[UseReporter(typeof(DiffReporter))]
	public class FileParserTest
	{
        public string CompletedPath { get { return PathUtilities.GetDirectoryForCaller()+@"..\..\Recipes\Recipes\Completed\"; }
        }

		[TestMethod]
		public void TestCreateRecipe()
		{
			var output = GetCompletedRecipe().CreateRecipe();
			Approvals.Verify(output.Item1);
		}

		[TestMethod]
		public void IfStatements()
		{
			var text = @"'  c1 --#4
If (guess = answer) Then
'        c2 --#1
    Sound.Play()
'    c3 --#6
ElseIf (guess > answer) Then 
'       c4 --#5
    MessageBox.ShowMessage()
EndIf";
			var lines = GetLines(text);
			var recipe = CompletedRecipe.CreateCompletedRecipeWithException(lines);
			Approvals.Verify(recipe.Item1);
		}

		[TestMethod]
		[Ignore]
		public void DemoHighLow()
		{
			var output = new CompletedRecipe(CompletedPath + "HighLow.completed.smallbasic").CreateRecipe();
			Approvals.Verify(output.Item1);
		}

		[TestMethod]
		public void TestCompleteRecipe()
		{
			var recipe = GetCompletedRecipe();
			Approvals.Verify(recipe.ToString());
		}

		[TestMethod]
		public void TestRecipeDirectory()
		{
			var recipe = GetCompletedRecipe();
			string expected =PathUtilities.GetDirectoryForCaller()+@"..\..\Recipes\Recipes\SimpleSquare.smallbasic";
			Assert.AreEqual(expected, recipe.RecipeFile);
		}

		[TestMethod]
		public void TestProcessedFileIsChanged()
		{
			var recipe = GetCompletedRecipe();
			var fileWritten = recipe.OutputRecipe(s => { });
			Assert.IsFalse(fileWritten);
		}

		[TestMethod]
		public void TestMissingLineNumbers()
		{
			var lines = GetLines("'ClearTheScreen (Recipe below)");
			var recipe = CompletedRecipe.CreateCompletedRecipeWithException(lines);
			Assert.AreEqual("Missing order number at end of comment [-- 3]", recipe.Item2[0]);
		}

		private string[] GetLines(string text)
		{
			return (text + "\r\n ").Split('\n');
		}

		[TestMethod]
		public void TestGetAllRecipes()
		{
			var all = CompletedRecipe.GetAll(CompletedPath);
			Approvals.VerifyAll(all, r => r.Name + "\r\n");
		}

		[TestMethod]
		[Ignore]
		public void DemoConvertAllRecipes()
		{
			RecipeUtils.ConvertAll(CompletedPath);
		}
		[TestMethod]
		[Ignore]
		public void DemoConvertSingleRecipes()
		{
			new CompletedRecipe(CompletedPath + "SimpleSquare.completed.smallbasic").OutputRecipe();
		}

		private CompletedRecipe GetCompletedRecipe()
		{
			return new CompletedRecipe(CompletedPath + "SimpleSquare.completed.smallbasic");
		}
	}

	public static class RecipeUtils
	{
		public static void ConvertAll(string basePath)
		{
			var all = CompletedRecipe.GetAll(basePath);
			foreach (var recipe in all)
			{
				recipe.OutputRecipe();
			}
		}
	}
}
